package com.Shop.Center.controller;

import com.Shop.Center.entity.ItemOrder;
import com.Shop.Center.entity.OrderItem;
import com.Shop.Center.service.AdminLoginService;
import com.Shop.Center.service.AdminUserService;
import com.Shop.Center.service.Login;
import com.Shop.Center.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class AdminOrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private AdminLoginService adminLoginService;

    @ResponseBody
    @PostMapping("/login_order")
    public Boolean runLogin(@RequestParam("name") String name, @RequestParam("password") String password){
        return adminLoginService.user_login(name, password);
    }
    /**
     * 列表
     */
    @GetMapping("/orders/list")
    @ResponseBody
    public List<ItemOrder> list() {
        if(AdminLoginService.is_login)
        return orderService.getAllOrders();
        return null;
    }

    /**
     * 修改
     */
    @PostMapping("/orders/update")
    @ResponseBody
    public int update(@RequestBody ItemOrder itemOrder) {
        if (Objects.isNull(itemOrder.getTotalPrice())
                || Objects.isNull(itemOrder.getOrderId())
                || itemOrder.getOrderId() < 1
                || itemOrder.getTotalPrice() < 1
                || StringUtils.isEmpty(itemOrder.getUserAddress())
                || !AdminLoginService.is_login) {
            return -1;
        }
        return orderService.updateOrderInfo(itemOrder);
    }
    //{ "orderId":1,"totalPrice":200,"userAddress":"2-1-502","orderStatus":"0" }

    /**
     * 详情
     */
    @GetMapping("/order-items/{id}")
    @ResponseBody
    public List<OrderItem> info(@PathVariable("id") Long id) {
        if(AdminLoginService.is_login)
            return orderService.orderDetail(id);
        return null;
    }
    //{"id":1}

    /**
     * 配货
     */
    @PostMapping("/orders/checkDone")
    @ResponseBody
    public List<ItemOrder> checkDone(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        List<Long> ids = JSON.parseArray(jsonObject.getJSONArray("ids").toJSONString(),Long.class);
        if (ids.size() < 1||!AdminLoginService.is_login) return null;
        return orderService.checkDone(ids);
    }
    //{"ids":[1,2]}

    /**
     * 出库
     */
    @PostMapping("/orders/checkOut")
    @ResponseBody
    public List<ItemOrder> checkOut(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        List<Long> ids = JSON.parseArray(jsonObject.getJSONArray("ids").toJSONString(),Long.class);
        if (ids.size() < 1||!AdminLoginService.is_login) return null;
        return orderService.checkOut(ids);
    }
    //{"ids":[1,2]}

    /**
     * 关闭订单
     */
    @PostMapping("/orders/close")
    @ResponseBody
    public int closeOrder(@RequestBody String json) {
        if(AdminLoginService.is_login) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            List<Long> ids = JSON.parseArray(jsonObject.getJSONArray("ids").toJSONString(), Long.class);
            return orderService.closeOrder(ids);
        }
        return -1;
    }
    //{"ids":[2]}
}
